
import o from "./EngineUtil"
import i from "./EventMgr"
import r from "./SystemConfig"
import n from "./NativeEventType"
import l from "./AdvertEventType"
import s from "./frameworkManager"
import c from "./AdManager"
import u from "./SdkHelper"



const { ccclass, property } = cc._decorator;

@ccclass
export default class CallAndroid {
    private static _instance: CallAndroid;

    public static getInstance(): CallAndroid {
        if (!this._instance) {
            this._instance = new CallAndroid();
        }
        return this._instance;
    }

    public getClientInfo(): string {
        const info = jsb.reflection.callStaticMethod("JavaScriptHelper", "getClientInfo", "()Ljava/lang/String;");
        cc.log("设备信息------");
        cc.log(info);
        return info;
    }

    public getCookieInfo(): string {
        return jsb.reflection.callStaticMethod("JavaScriptHelper", "getCookieInfo", "()Ljava/lang/String;");
    }

    public ysdkLogin(): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "ysdkLogin", "()V");
    }

    public wxLogin(): void {
        console.log("调用android微信登陆api");
        jsb.reflection.callStaticMethod("JavaScriptHelper", "wxLogin", "()V");
    }

    public showSplashAd(e: number): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "loadSplashAd", "(II)V", 0, e);
    }

    public closeSplashAd(): void {
        cc.log("android.closeSplashAd");
        jsb.reflection.callStaticMethod("JavaScriptHelper", "closeSplashAd", "()V");
    }

    public showRewardVideoAd(e: string): void {
        cc.log("android.showRewardVideoAd", e);
        jsb.reflection.callStaticMethod("JavaScriptHelper", "showRewardVideoAd", "(Ljava/lang/String;)V", e);
    }

    public preLoadImgAd(e: number): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "preLoadImgAd", "(I)V", e);
    }

    public showImgAd(e: number, t: number, a: number, o: number, i: number, r: number): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "showImgAd", "(IIIIII)V", e, t, a, o, i, r);
    }

    public closeImgAd(): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "closeImgAd", "()V");
    }

    public preLoadBannerAd(e: number): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "preLoadBannerAd", "(I)V", e);
    }

    public showBannerAd(e: number, t: number, a: number, o: number, i: number, r: number): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "showBannerAd", "(IIIIII)V", e, t, a, o, i, r);
    }

    public closeBannerAd(): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "closeBannerAd", "()V");
    }

    public isNetworkAvailable(): number {
        const result = jsb.reflection.callStaticMethod("JavaScriptHelper", "isNetworkAvailable", "()I");
        return Number(result);
    }

    public showToast(e: string): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "showToast", "(Ljava/lang/String;)V", e);
    }

    public showForceDialog(e: string, t: string): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "showForceDialog", "(Ljava/lang/String;Ljava/lang/String;)V", e, t);
    }

    public finishApp(): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "finishActivity", "()V");
    }

    public setEnterAgreementTime(): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "setEnterAgreementTime", "()V");
    }

    public getFirstLaunchTime(): number {
        const result = jsb.reflection.callStaticMethod("JavaScriptHelper", "reportAppFirstLaunchTime", "()Ljava/lang/String;");
        cc.log("getFirstLaunchTime===" + result);
        return result || 0;
    }

    public getEnterAgreementTime(): number {
        const result = jsb.reflection.callStaticMethod("JavaScriptHelper", "reportAgreementTime", "()Ljava/lang/String;");
        cc.log("getEnterAgreementTime====" + result);
        return result || 0;
    }

    public showNotification(e: string): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "showNotification", "(Ljava/lang/String;)V", e);
    }

    public getForceSlotId(): number {
        return jsb.reflection.callStaticMethod("JavaScriptHelper", "getForceSlotId", "()I");
    }

    public getNormalSlotId(): number {
        return jsb.reflection.callStaticMethod("JavaScriptHelper", "getNormalSlotId", "()I");
    }

    public reportData(e: string, t: any, a: boolean = false): void {
        const i = {};
        i.eventName = e;
        const n = [];
        n.push({ paramName: "ts", paramValue: o.getTimeStamp() });
        n.push({ paramName: "game_name", paramValue: r.GAME_NAME });
        if (t) {
            for (const l of Object.keys(t)) {
                const u = {};
                u.paramName = l;
                u.paramValue = t[l];
                n.push(u);
            }
        }
        i.param = n;
        const p = JSON.stringify(i);
        if (cc.sys.isNative) {
            if (a) {
                jsb.reflection.callStaticMethod("JavaScriptHelper", "reportCoreData", "(Ljava/lang/String;)V", p);
                cc.log("android", a + "埋点>>>>>>>>>>" + p);
            } else {
                jsb.reflection.callStaticMethod("JavaScriptHelper", "reportData", "(Ljava/lang/String;)V", p);
                cc.log("android", a + "埋点>>>>>>>>>>" + p);
            }
        }
    }

    public getBlackBox(): string {
        return jsb.reflection.callStaticMethod("JavaScriptHelper", "getBlackBox", "()Ljava/lang/String;");
    }

    public getScreenWidth(): number {
        return jsb.reflection.callStaticMethod("JavaScriptHelper", "getScreenWidth", "()I");
    }

    public getScreenHeight(): number {
        return jsb.reflection.callStaticMethod("JavaScriptHelper", "getScreenHeight", "()I");
    }

    public hasNotchInScreen(): boolean {
        const result = jsb.reflection.callStaticMethod("JavaScriptHelper", "hasNotchInScreen", "()Z");
        cc.log("android.hasNotchInScreen", result);
        return result;
    }

    public getNotchHeight(): number {
        const statusBarHeight = jsb.reflection.callStaticMethod("JavaScriptHelper", "getStatusBarHeight", "()I");
        cc.log("android.getStatusBarHeight", statusBarHeight);
        const frameSize = cc.view.getFrameSize();
        return statusBarHeight * cc.winSize.height / frameSize.height;
    }

    public setVibrator(e: number): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "setVibrator", "(I)V", e);
    }

    public cancelVibrator(): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "cancelVibrator", "()V");
    }

    public openKefu(e: string, t: string, a: string): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "openKefu", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", e, t, a);
    }

    public setUserInfo(e: string): void {
        cc.log("android.setUserInfo---", e);
        jsb.reflection.callStaticMethod("JavaScriptHelper", "setUserInfo", "(Ljava/lang/String;)V", e);
    }

    public getNgister(e: string, t: string, a: string): string {
        cc.log("url====", e);
        return jsb.reflection.callStaticMethod("JavaScriptHelper", "getNgister", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;", e, t, a);
    }

    public getAesEncrypData(e: string): string {
        return jsb.reflection.callStaticMethod("JavaScriptHelper", "getAesEncrypData", "(Ljava/lang/String;)Ljava/lang/String;", e);
    }

    public getAesDncrypData(e: string): string {
        return jsb.reflection.callStaticMethod("JavaScriptHelper", "getAesDncrypData", "(Ljava/lang/String;)Ljava/lang/String;", e);
    }

    public initOtherSDK(): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "initOtherSDK", "()V");
    }

    public getChannelName(): string {
        return jsb.reflection.callStaticMethod("JavaScriptHelper", "getChannelName", "()Ljava/lang/String;");
    }

    public getVersionName(): string {
        return jsb.reflection.callStaticMethod("JavaScriptHelper", "getVersionName", "()Ljava/lang/String;");
    }

    public getVersionCode(): number {
        return jsb.reflection.callStaticMethod("JavaScriptHelper", "getVersionCode", "()I");
    }

    public isRunningInVirtualApk(): boolean {
        return jsb.reflection.callStaticMethod("JavaScriptHelper", "isRunningInVirtualApk", "()Z");
    }

    public isRoot(): boolean {
        return jsb.reflection.callStaticMethod("JavaScriptHelper", "isRoot", "()Z");
    }

    public isEmulator(): boolean {
        console.log("isEmulator");
        return jsb.reflection.callStaticMethod("JavaScriptHelper", "isEmulator", "()Z");
    }

    public requestSMId(): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "requestSMId", "()V");
    }

    public getSMId(e: string): void {
        i.trigger(n.ON_GET_SM_ID, e);
    }

    public requestTDId(): string {
        return jsb.reflection.callStaticMethod("JavaScriptHelper", "requestTDId", "()Ljava/lang/String;");
    }

    public getBdDid(): string {
        const result = jsb.reflection.callStaticMethod("JavaScriptHelper", "getBd_did", "()Ljava/lang/String;");
        return result === "null" ? "" : result;
    }

    public getActivityNumByDate(e: string): number {
        return jsb.reflection.callStaticMethod("JavaScriptHelper", "getActivityNumByDate", "(Ljava/lang/String;)I", e);
    }

    public showForceToast(e: string): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "showForceToast", "(Ljava/lang/String;)V", e);
    }

    public loadNewSplashAd(e: number, t: number): void {
        u.reportData("loadNewSplashAd", { type: e });
        console.log("loadNewSplashAd", e);
        jsb.reflection.callStaticMethod("JavaScriptHelper", "loadNewSplashAd", "(II)V", e, t);
    }

    public getHSToken(): string {
        const result = jsb.reflection.callStaticMethod("JavaScriptHelper", "getHSToken", "()Ljava/lang/String;");
        console.log("获取火山getHSToken:", result);
        return result === "null" ? "" : result;
    }

    public onGetHSToken(e: string): void {
        console.log("获取火山dev_token", e);
        u.initBD(e);
    }

    public onGetBdDid(e: string): void {
        console.log("获取火山id", e);
        u.initBD(e);
    }

    public getOAID(): void {
        console.log("----Android回调,获取oaid完成==1111");
        i.trigger(n.OAID_FINISH);
    }

    public onGetOAID(): void {
        console.log("----Android回调,获取oaid完成==2222");
        i.trigger(n.OAID_FINISH);
    }

    public getMiddleConfig(): string {
        const result = jsb.reflection.callStaticMethod("JavaScriptHelper", "getConfig", "()Ljava/lang/String;");
        cc.log("Android获取中台配置,getMiddleConfig", result);
        return result;
    }

    public onGetAdInfo(e: string): void {
        const info = JSON.parse(e);
        cc.log("获取cpm回调=======", info);
        i.trigger(l.ONGETADINFO, info);
    }

    public onGetAuthorityFinish(): void {
        console.log("Android回调,获取用户权限完成");
        i.trigger(n.AUTHOR_FINISH);
    }

    public onVideoError(e: string): void {
        cc.log("Java调用JsonVideoError>>", e);
        i.trigger(l.VIDEO_ERROR, JSON.parse(e).data);
    }

    public onVideoClose(e: string): void {
        cc.log("Java调用JsonVideoClose>>", e);
        i.trigger(l.VIDEO_CLOSE, JSON.parse(e).data);
    }

    public onVideoFinish(e: string): void {
        cc.log("Java调用JsonVideoFinish>>", e);
        i.trigger(l.VIDEO_FINISH);
    }

    public onVideoOpensuccess(e: string): void {
        cc.log("Java调用JsonVideoOpensuccess>>", e);
        i.trigger(l.VIDEO_OPEN_SUCCESS, e);
    }

    public onGetWechatCode(e: string): void {
        cc.log("Java调用JsonGetWechatCode", e);
        i.trigger(n.GET_WECHAT_CODE, e);
    }

    public onGetConfig(e: string): void {
        cc.log("Java调用Js中台配置onGetConfig", e);
        i.trigger(n.ON_GET_MIDDLE_CONFIG, e);
    }

    public onSplashAdFinish(): void {
        cc.log("android.onSplashAdFinish");
        i.trigger(l.SPLASH_FINISH);
    }

    public onAppStart(): void {
        cc.log("Java调用JsonAppStart");
        i.trigger(n.APP_START);
    }

    public onAppRestart(): void {
        cc.log("Java调用JsonAppReStart");
        i.trigger(n.APP_RESTART);
    }

    public onAppPause(): void {
        cc.log("Java调用JsonAppPause");
        i.trigger(n.APP_PAUSE);
    }

    public onAppResume(): void {
        cc.log("Java调用JsonAppResume");
        i.trigger(n.APP_RESUME);
    }

    public onAppStop(): void {
        cc.log("Java调用JsonAppStop");
        i.trigger(n.APP_STOP);
    }

    public onAppDestory(): void {
        cc.log("Java调用JsonAppDestory");
        i.trigger(n.APP_DESTROY);
    }

    public preloadWfUnionInteractionAd(): void {
        console.log("preloadWfUnionInteractionAd");
        return jsb.reflection.callStaticMethod("JavaScriptHelper", "preloadWfUnionInteractionAd", "()V");
    }

    public showInsertAd(): void {
        jsb.reflection.callStaticMethod("JavaScriptHelper", "showInsertAd", "()V");
    }

    public onInsertAdClick(): void {
        console.log("onInsertAdClick");
    }

    public onInsertAdClose(): void {
        console.log("onInsertAdClose");
        c.getInstance().onInsertAdClose();
    }

    public onInsertAdShow(): void {
        console.log("onInsertAdShow");
    }

    public onBackPressed(e: string): void {
        o.log("onBackPressed\n" + e);
    }
}

window["callAndroid"] = CallAndroid.getInstance();